//
//  SISSContractOverduePaymentDutyCell.m
//  SISSAPP
//
//  Created by 袁应荣 on 2017/10/9.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSContractAlternativeFillInCell.h"

@interface SISSContractAlternativeFillInCell()<UITextFieldDelegate,UITextViewDelegate>

/// checkOneButton
@property (weak, nonatomic) UIButton *checkOneButton;
/// checkOneButton
@property (weak, nonatomic) UIButton *checkTwoButton;

/// oneLabel
@property (weak, nonatomic) YYLabel *oneLabel;
/// twoLabel
@property (weak, nonatomic) YYLabel *twoLabel;

/// textFieldArrM
@property (strong, nonatomic) NSMutableArray *textFieldArrM;
/// textFieldArrM
@property (strong, nonatomic) NSMutableArray *textFieldArrMTwo;

/* oneLine */
@property (weak, nonatomic) UIView *oneLine;

/// checkedButton
@property (weak, nonatomic) UIButton *checkedButton;

@end
@implementation SISSContractAlternativeFillInCell

- (void)setContractModel:(SISSContractModel *)contractModel {
    [super setContractModel:contractModel];
    
    self.checkOneButton.selected = NO;
    self.checkTwoButton.selected = NO;
    self.checkedButton = contractModel.firstIndex == 1 ? self.checkOneButton : (contractModel.firstIndex == 2 ? self.checkTwoButton : nil);
    self.checkedButton.selected = self.checkedButton ? YES : NO;
    
    self.oneLine.hidden = contractModel.isHiddenBottomLine;
    if (!contractModel.strArr) return;
    if (self.textFieldArrM.count > 0) [self.textFieldArrM removeAllObjects];
    for (NSInteger i = 1; i < contractModel.strArr.count; i++) {
        UITextField *textField = [[UITextField alloc] init];
        textField.text = contractModel.firstIndex == 1 ? contractModel.rightStr : @"";;
        
        [self.textFieldArrM addObject:textField];
    }
    NSMutableArray *strArrM = [NSMutableArray arrayWithArray:contractModel.strArr];
    [strArrM removeObjectAtIndex:0];
    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:contractModel.strArr.firstObject attributes:nil];
    self.oneLabel.attributedText = [self getTextAttritedStrWithAttachText:attributedText strs:strArrM textFields:self.textFieldArrM keyboardType:UIKeyboardTypeDecimalPad placeHolders:@[contractModel.placeHolder ? contractModel.placeHolder : @""]];
    // 设置TextField的编辑状态
    for (UITextField *textField in self.textFieldArrM) {
        textField.userInteractionEnabled = contractModel.firstIndex == 1;
    }
    
    if (self.textFieldArrMTwo.count > 0) [self.textFieldArrMTwo removeAllObjects];
    for (NSInteger i = 1; i < contractModel.strArrTwo.count; i++) {
        UITextField *textField = [[UITextField alloc] init];
        textField.text = contractModel.firstIndex == 2 ? contractModel.rightTwoStr : @"";;
        [self.textFieldArrMTwo addObject:textField];
    }
    NSMutableArray *strArrMTwo = [NSMutableArray arrayWithArray:contractModel.strArrTwo];
    [strArrMTwo removeObjectAtIndex:0];
    NSMutableAttributedString *attributedTextTwo = [[NSMutableAttributedString alloc] initWithString:contractModel.strArrTwo.firstObject attributes:nil];
    self.twoLabel.attributedText = [self getTextAttritedStrWithAttachText:attributedTextTwo strs:strArrMTwo textFields:self.textFieldArrMTwo keyboardType:[SISSPublicMethods matchKeybordTypeWithKeyboardNum:contractModel.keyboardNum > 0 ? contractModel.keyboardNum : 9] placeHolders:@[contractModel.placeHolderTwo ? contractModel.placeHolderTwo : @""]];

    // 设置TextField的编辑状态
    for (UITextField *textField in self.textFieldArrMTwo) {
        textField.userInteractionEnabled = 2;
    }
}




#pragma mark - 点击事件
- (void)checkButtonClickAction:(UIButton *)button {
    self.checkedButton.selected = NO;
    self.checkedButton = button;
    self.checkedButton.selected = !button.selected;

    for (UITextField *textField in self.textFieldArrM) {
        textField.userInteractionEnabled = button.tag == 100;
        textField.text = button.tag == 100 ? textField.text : @"";
    }
    for (UITextField *textField in self.textFieldArrMTwo) {
        textField.userInteractionEnabled = button.tag == 101;
        textField.text = button.tag == 101 ? textField.text : @"";
    }
   
    self.contractModel.firstIndex = button.tag - 99;
}


- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    for (UITextField *tempTextField in self.textFieldArrM) {
        if (tempTextField == textField) {
            self.contractModel.rightStr = textField.text;
        }
    }
    
    for (UITextField *tempTextField in self.textFieldArrMTwo) {
        if (tempTextField == textField) {
            self.contractModel.rightTwoStr = textField.text;
        }
    }
    
}


#pragma mark - UI
- (void)initUI {
    [super initUI];
    
    UIButton *button = [self creatButtonWithTag:100];
    [self.contentView addSubview:button];
    self.checkOneButton = button;
    
    YYLabel *oneLabel = [[YYLabel alloc] init];
    oneLabel.numberOfLines = 0;
    oneLabel.preferredMaxLayoutWidth = (SCREEN_WIDTH - 56);
    oneLabel.textColor = SECONDARY_TEXT_COLOR;
    oneLabel.font = [UIFont systemFontOfSize:16];
    [self.contentView addSubview:oneLabel];
    self.oneLabel = oneLabel;
    
    UIView *oneLine = [[UIView alloc] init];
    oneLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.contentView addSubview:oneLine];
    self.oneLine = oneLine;
    
    UIButton *checkTwoButton = [self creatButtonWithTag:101];
    [self.contentView addSubview:checkTwoButton];
    self.checkTwoButton = checkTwoButton;
     
    YYLabel *twoLabel = [[YYLabel alloc] init];
    twoLabel.textColor = SECONDARY_TEXT_COLOR;
    twoLabel.preferredMaxLayoutWidth = (SCREEN_WIDTH - 56);
    twoLabel.numberOfLines = 0;
    twoLabel.userInteractionEnabled = YES;
    [self.contentView addSubview:twoLabel];
    self.twoLabel = twoLabel;
    
    UIView *bottomLine = [[UIView alloc] init];
    bottomLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.contentView addSubview:bottomLine];
    self.bottomLine = bottomLine;
    
    
    
}

#pragma mark - 约束
- (void)initConstraint {
    [super initConstraint];
    
    WeakSelf
    CGFloat margin = 8.0;
    
    [self.oneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.contentView).offset(2 * margin);
        make.right.equalTo(weakSelf.contentView).offset(-2 * margin);
        make.left.equalTo(weakSelf.contentView).offset(5 * margin);
    }];
    
    [self.checkOneButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.contentView).offset(1.5 * margin);
        make.centerY.equalTo(weakSelf.oneLabel);
    }];
    [self.oneLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.oneLabel.mas_bottom).offset(margin);
        make.left.right.equalTo(weakSelf.contentView);
        make.height.mas_equalTo(1);
    }];
    
    [self.twoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.oneLine.mas_bottom).offset(margin);
        make.left.right.equalTo(weakSelf.oneLabel);
    }];
    [self.checkTwoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.checkOneButton);
        make.centerY.equalTo(weakSelf.twoLabel);
    }];
  
    [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.twoLabel.mas_bottom).offset(margin);
        make.left.right.bottom.equalTo(weakSelf.contentView);
        make.height.mas_equalTo(1);
    }];
}

- (NSMutableArray *)textFieldArrM {
    if (!_textFieldArrM) {
        _textFieldArrM = [NSMutableArray array];
    }
    return _textFieldArrM;
}

- (NSMutableArray *)textFieldArrMTwo {
    if (!_textFieldArrMTwo) {
        _textFieldArrMTwo = [NSMutableArray array];
    }
    return _textFieldArrMTwo;
}

@end
